
package com.companymodule.dao;
import com.companymodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbCompanyDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbCompany tbCompany=null;
    private List<TbCompany> tbCompanyList=null;

    public boolean add(TbCompany inTbCompany){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Company(CompanyCode,CompanyTypeId,CompanyName,CompanyAddress,CompanyContactList,CompanyMobile,CompanyCreateTime,CreditRatingId,CompanyMemo,BankName,BankUser,BankCard,companyManScale) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbCompany.getCompanyCode());
        pt.setString(2,inTbCompany.getCompanyTypeId());
        pt.setString(3,inTbCompany.getCompanyName());
        pt.setString(4,inTbCompany.getCompanyAddress());
        pt.setString(5,inTbCompany.getCompanyContactList());
        pt.setString(6,inTbCompany.getCompanyMobile());
        pt.setString(7,inTbCompany.getCompanyCreateTime());
        if(inTbCompany.getCreditRatingId()==0)
        	pt.setNull(8, Types.INTEGER);
        else
        	pt.setInt(8, inTbCompany.getCreditRatingId());
        pt.setString(9,inTbCompany.getCompanyMemo());
        pt.setString(10,inTbCompany.getBankName());
        pt.setString(11,inTbCompany.getBankUser());
        pt.setString(12,inTbCompany.getBankCard());
        pt.setString(13, inTbCompany.getCompanyManScale());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();        
        }
        return flag;
    }
    public boolean del(int inCompanyId){
        return UtilDAO.del("Tb_Company", "CompanyId", inCompanyId);
    }
    public boolean edit(TbCompany inTbCompany){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Company set CompanyCode=?,CompanyTypeId=?,CompanyName=?,CompanyAddress=?,CompanyContactList=?,CompanyMobile=?,CompanyCreateTime=?,CreditRatingId=?,CompanyMemo=?,BankName=?,BankUser=?,BankCard=?,companyManScale=? where CompanyId=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbCompany.getCompanyCode());
        pt.setString(2,inTbCompany.getCompanyTypeId());
        pt.setString(3,inTbCompany.getCompanyName());
        pt.setString(4,inTbCompany.getCompanyAddress());
        pt.setString(5,inTbCompany.getCompanyContactList());
        pt.setString(6,inTbCompany.getCompanyMobile());
        pt.setString(7,inTbCompany.getCompanyCreateTime());
        if(inTbCompany.getCreditRatingId()==0)
        	pt.setNull(8, Types.INTEGER);
        else
        	pt.setInt(8, inTbCompany.getCreditRatingId());
        pt.setString(9,inTbCompany.getCompanyMemo());
        pt.setString(10,inTbCompany.getBankName());
        pt.setString(11,inTbCompany.getBankUser());
        pt.setString(12,inTbCompany.getBankCard());
        pt.setString(13, inTbCompany.getCompanyManScale());
        pt.setInt(14,inTbCompany.getCompanyId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbCompany getTbCompanyByCompanyId(int inCompanyId) {
        tbCompany=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Company where CompanyId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inCompanyId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbCompany == null)tbCompany = getTbCompany(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompany;
    }
    public List<TbCompany> getTbCompanyAll() {
        tbCompanyList=new ArrayList<TbCompany>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Company";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyList.add(getTbCompany(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyList;
    }
    public List<TbCompany> getTbCompanyAll(VskPage vp) {
        tbCompanyList=new ArrayList<TbCompany>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Company";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Company", "CompanyId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyList.add(getTbCompany(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyList;
    }
    public TbCompany getTbCompany(ResultSet rs) throws Exception{
        TbCompany tbCompany = new TbCompany();
        tbCompany.setCompanyId(rs.getInt("CompanyId"));
        tbCompany.setCompanyCode(rs.getString("CompanyCode"));
        tbCompany.setCompanyTypeId(rs.getString("CompanyTypeId"));
        tbCompany.setCompanyName(rs.getString("CompanyName"));
        tbCompany.setCompanyAddress(rs.getString("CompanyAddress"));
        tbCompany.setCompanyContactList(rs.getString("CompanyContactList"));
        tbCompany.setCompanyMobile(rs.getString("CompanyMobile"));
        tbCompany.setCompanyCreateTime(rs.getString("CompanyCreateTime"));
        tbCompany.setCompanyManScale(rs.getString("CompanyManScale"));
        tbCompany.setCreditRatingId(rs.getInt("CreditRatingId"));
        tbCompany.setCompanyMemo(rs.getString("CompanyMemo"));
        tbCompany.setBankName(rs.getString("BankName"));
        tbCompany.setBankUser(rs.getString("BankUser"));
        tbCompany.setBankCard(rs.getString("BankCard"));
        tbCompany.setCreateTime(rs.getString("CreateTime"));
        return tbCompany;
    }
    public static void main(String[] args) {
    	List<TbCompany> tclist=new TbCompanyDAO().getTbCompanyAll();
    	for(int i=0;i<tclist.size();i++){
    		TbCompany tc=tclist.get(i);
    		System.out.println(tc.getCompanyManScale());
    	}//end 
	}
}
